<?php /*
Copyright 2002 Dan Boitnott (dboitnot@yahoo.com)

This file is part of phpTasks.

phpTasks is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

phpTasks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with phpTasks; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/ ?>
<?php
require("common.php");
// dump_hash($HTTP_VARS);
if (isset($HTTP_VARS["action"])) {
	switch ($HTTP_VARS["action"]) {
	case "Cancel":
		redirect("index.php");
		break;
  	case "Ok":
		switch ($HTTP_VARS["type"]) {
		case "open":
			$cond_type = "completed IS NULL";
   			break;
      		case "completed":
			$cond_type = "completed IS NOT NULL";
			break;
   		case "both":
			$cond_type = "TRUE";
			break;
   		}
		$cond_assigned_to = ($HTTP_VARS["assigned_to"] == "0") ? "TRUE" : ("assigned_to=" . $HTTP_VARS["assigned_to"]);
		$cond_assigned_by = ($HTTP_VARS["assigned_by"] == "0") ? "TRUE" : ("assigned_by_id=" . $HTTP_VARS["assigned_by"]);
		if ($HTTP_VARS["created_preset"] == "Date Range") {
			$cond_created = "created BETWEEN '" . $HTTP_VARS["created_start_year"] . "-" . $HTTP_VARS["created_start_month"] . "-" . $HTTP_VARS["created_start_day"] . "' AND '" . $HTTP_VARS["created_end_year"] . "-" . $HTTP_VARS["created_end_month"] . "-" . $HTTP_VARS["created_end_day"] . "'";
		} else {
			$cond_created = str_replace("\\", "", $HTTP_VARS["created_preset"]);
		}
		if ($HTTP_VARS["type"] != "open") {
			if ($HTTP_VARS["completed_preset"] == "Date Range") {
				$cond_completed = "completed BETWEEN '" . $HTTP_VARS["completed_start_year"] . "-" . $HTTP_VARS["completed_start_month"] . "-" . $HTTP_VARS["completed_start_day"] . "' AND '" . $HTTP_VARS["completed_end_year"] . "-" . $HTTP_VARS["completed_end_month"] . "-" . $HTTP_VARS["completed_end_day"] . "'";
			} else {
				$cond_completed = str_replace("\\", "", $HTTP_VARS["completed_preset"]);
			}
		} else
			$cond_completed = "TRUE";
		$sql = "SELECT * FROM task_list WHERE $cond_type AND $cond_assigned_to AND $cond_assigned_by AND $cond_created AND $cond_completed ORDER BY " . $HTTP_VARS["orderby_field"] . " " . $HTTP_VARS["orderby_method"];
		std_header($HTTP_VARS["title"]);
		//dump_hash($HTTP_VARS);
		//echo ($sql);
		$res = pg_exec($sql);
		$rc = pg_numrows($res);
		echo ("<center><h1>" . $HTTP_VARS["title"] . "</h1></center>");
		echo ("<center><em>" . date("l F j, Y - g:i") . substr(date("a"), 0, 1) . "</em></center>");
		echo ("<center>Count: $rc</center>");
		switch($HTTP_VARS["style"]) {
		case "Quick List":
			break;
		case "Detailed List":
			echo ("<table border=0 align='center'>");
			for ($rnum = 0; $rnum < $rc; $rnum++) {
				$r = get_row($res, $rnum);
?>
				<tr><td colspan=4 style="page-break-before:always"><hr></td></tr>
				<tr valign=top style="page-break-before:avoid">
					<td nowrap align='right'><strong>Title:</strong></td>
					<td align='left' colspan=3><?=$r["title"]?></td>
				</tr><tr valign=top style="page-break-before:avoid">
					<td nowrap align='right'><strong>Assigned:</strong></td>
					<td align='left'><?=$r["created"]?></td>
					<td nowrap align='right'><strong>Status:</strong></td>
					<td align='left'><?=$r["completed"] ? "Completed " . $r["completed"] : "Open" ?></td>
     				</tr><tr valign=top style="page-break-before:avoid">
					<td nowrap align='right'><strong>Assigned By:</strong></td>
					<td align='left'><?=$r["assigned_by"]?></td>
					<td nowrap align='right'><strong>Assigned To:</strong></td>
					<td align='left'><?=$r["assigned_to_name"]?></td>
     				</tr><tr valign=top style="page-break-before:avoid">
					<td nowrap align='right'><strong>Task #:</strong>
					<td align='left'><?=$r["taskid"]?></td>
					<td nowrap align='right'><strong>Priority:</strong></td>
					<td align='left'><?=$r["priority_name"]?></td>
     				</tr><tr valign=top style="page-break-before:avoid">
					<td nowrap align='right'><strong>Description:</strong></td>
					<td align='left' colspan=3><?=html_encode($r["description"])?></td>
     				</tr><tr valign=top style="page-break-before:avoid">
					<td nowrap align='right'><strong>Log:</strong></td>
					<td algin='left' colspan=3>
<?php
					$sql = "SELECT * FROM tasklog_list WHERE taskid=" . $r["taskid"] . " ORDER BY created";
					$log_res = pg_exec($conn, $sql);
					$log_rc = pg_numrows($log_res);
					if ($log_rc > 0) {
						echo ("<table border='0'>");
						for ($log_rnum = 0; $log_rnum < $log_rc; $log_rnum++) {
							$l = get_row($log_res, $log_rnum, "daydatetime");
							if ($log_rnum > 0)
								echo ("<tr><td></td></tr>");
							echo ("<tr class=log_header><td align='left' style='page-break-before:avoid'>" . $l["user"] . " - " . $l["created"] . "</td></tr>");
							echo ("<tr class=log_entry style='background-color:transparent' style='page-break-before:avoid'><td align='left'>" . html_encode($l["entry"]) . "</td></tr>");
						}
						echo ("</table>");
					} else
						echo ("No Entries");
?>
					</td>
				</tr>
<?php
     			}
			echo ("</table>");
			break;
   		}
		std_footer();
		break;
	}
} else {
	std_header("Report");
?>
<h1>Configure Report</h1>
<form action='report.php' method=POST>
<table border=0>
<tr>
	<td colspan=2><h2>Select Tasks...</h2></td>
</tr><tr>
	<td align='right'><strong>Task Type:</strong></td>
	<td align='left'>
		<select name='type'>
			<option value='open'>Open</option>
			<option selected value='completed'>Completed</option>
			<option value='both'>Open & Completed</option>
		</select>
	</td>
</tr><tr>
	<td align='right'><strong>Assigned To:</strong></td>
	<td align='left'>
<?php
		if ($USER["viewalltasks"])
			std_select($conn, "assigned_to", "SELECT userid, name FROM users ORDER BY name", "name", "userid", $tl_userid, false, "1", array("0" => "All Users"), true);
		else {
			echo ("<input type=hidden name='assigned_to' value='" . $USER["userid"] . "'>");
			echo ($USER["name"]);
   		}
?>
	</td>
</tr><tr>
	<td align='right'><strong>Assigned By:</strong></td>
	<td align='left'><?php std_select($conn, "assigned_by", "SELECT userid, name FROM users ORDER BY name", "name", "userid", "0", false, "1", array("0" => "All Users"), true) ?></td>
</tr><tr>
	<td align='right'><strong>Assigned:</strong></td>
	<td align='left'><?php date_ranger("created", "created", "Any Day") ?></td>
</tr><tr>
	<td align='right'><strong>Completed:</strong></td>
	<td align='left'><?php date_ranger("completed", "completed", "This Week") ?></td>
</tr><tr>
	<td colspan=2><h2>Report Style...</h2></td>
</tr><tr>
	<td align='right'><strong>Title:</strong>
	<td align='left'><input type=text size=30 name='title' value='Task Report'></td>
</tr><tr>
	<td align='right'><strong>Type:</strong>
	<td align='left'>
		<select name='style'>
			<?php /* ?> <option>Quick List</option> <?php */ ?>
			<option selected>Detailed List</option>
		</select>
	</td>
</tr><tr>
	<td align='right'><strong>Sort By:</strong></td>
	<td align='left'><?php sort_by_selector("orderby", "completed"); ?>
</tr><tr>
	<td></td>
	<td align='left'>
		<input type=submit name='action' value='Ok'>
		<input type=submit name='action' value='Cancel'>
	</td>
</tr>
</table>
</form>
<?php
	std_footer();
}
?>